import Sequelize from 'sequelize';
import express from 'express';
import { statusList, sequelizeStatus } from '../../common/status'; // 状态码对照
import { User } from '../../modal/index'; // 数据库实体类
import { checkPermission } from '../../utils/utils'; // 工具类

const router = express.Router();
const permission = ["admin"];

router.post('/', async function (Request, Response, next) {
  //权限校验
  await checkPermission(Request, Response, permission)
  let param = Request.body;
  let querySql = {
    [Sequelize.Op.and]: []
  }
  param.username && querySql[Sequelize.Op.and].push({
    username: {
      [Sequelize.Op.like]: `%${param.username}%`
    }
  })
  User.Modal.findAndCountAll({
    where: querySql,
    offset: (param.pageNo - 1) * param.pageSize,
    limit: param.pageSize, // 分页参数
    attributes: { exclude: ['password'] } // 过滤字段
  }).then(res => {
    Response.json({
      data: {
        list: res.rows,
        total: res.count,
      },
      message: "success"
    });
  }).catch(err => {
    console.log(err)
    Response.status(500).send({ message: sequelizeStatus[err.name] || statusList[500] || err.name });
  });


});



module.exports = router;